package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.account.enums.CodeEnum;
import com.ruoyi.account.service.ISxoSafetyLogService;
import com.ruoyi.account.util.HttpResult;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.IBaseClient;
import com.ruoyi.system.api.impl.BankClearQueryApi;
import com.ruoyi.system.api.impl.CustAcctIdBalanceQueryApi;
import com.ruoyi.system.config.PayConfig;
import com.ruoyi.system.config.TaskExecutePoolConfig;
import com.ruoyi.system.model.dto.BankClearQueryDto;
import com.ruoyi.system.model.vo.AcctArrayVo;
import com.ruoyi.system.model.vo.BankClearQueryVo;
import com.ruoyi.system.model.vo.CustAcctIdBalanceQueryVo;
import com.ruoyi.system.util.BusinessException;
import com.ruoyi.system.util.PaPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BankPlatformMapper;
import com.ruoyi.system.domain.BankPlatform;
import com.ruoyi.system.service.IBankPlatformService;

/**
 * 银行账户平台Service业务层处理
 * 
 * @author zhangjf
 * @date 2025-10-13
 */
@Slf4j
@Service
public class BankPlatformServiceImpl extends BasePaServiceImpl implements IBankPlatformService
{
    @Autowired
    private BankPlatformMapper bankPlatformMapper;

    @Autowired
    private ISxoSafetyLogService sxoSafetyLogService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private TaskExecutePoolConfig taskExecutePoolConfig;



    @Override
    public BankClearQueryVo bankClearQuery(BankClearQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6108");
        json.put("FunctionFlag", "2");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        log.info("资金汇总账号:"+payConfig.getFundSummaryAcctNo());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());
        json.put("PageNum", "1");



        try {
            IBaseClient baseClient = (IBaseClient) new BankClearQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            log.info("6108在途清算调平安入参："+json);
            log.info("6108在途清算调平安入参时间："+ DateUtil.now());
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            log.info("6108在途清算调平安回参时间："+DateUtil.now());
            log.info("6108在途清算调平安回参："+extenddata);

            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankClearQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    /**
     * 查询账户子账户余额
     * @param
     * @return
     */
    @Override
    public Object  custAcctIdBalanceQuery(String sxfSubacct) {

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6010");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", sxfSubacct);
        json.put("QueryFlag", "2");
        json.put("PageNum", "1");

        try {
        IBaseClient baseClient = new CustAcctIdBalanceQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        JSONObject extenddata = handleError(httpResult);
        JSONObject obj = buildResult(extenddata);
        CustAcctIdBalanceQueryVo vo = JSONObject.parseObject(obj.toJSONString(), CustAcctIdBalanceQueryVo.class);
        List<AcctArrayVo> AcctArray = vo.getAcctArray();
        return AcctArray.get(0).getAcctAvailBal();

        } catch (Exception e) {

        throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
}
    }

    /**
     * 查询银行账户平台
     * 
     * @param bankPlatformAcctid 银行账户平台主键
     * @return 银行账户平台
     */
    @Override
    public BankPlatform selectBankPlatformByBankPlatformAcctid(Long bankPlatformAcctid)
    {
        return bankPlatformMapper.selectBankPlatformByBankPlatformAcctid(bankPlatformAcctid);
    }

    /**
     * 查询银行账户平台列表
     * 
     * @param bankPlatform 银行账户平台
     * @return 银行账户平台
     */
    @Override
    public List<BankPlatform> selectBankPlatformList(BankPlatform bankPlatform)
    {
        return bankPlatformMapper.selectBankPlatformList(bankPlatform);
    }

    /**
     * 新增银行账户平台
     * 
     * @param bankPlatform 银行账户平台
     * @return 结果
     */
    @Override
    public int insertBankPlatform(BankPlatform bankPlatform)
    {
        bankPlatform.setCreateTime(DateUtils.getNowDate());
        return bankPlatformMapper.insertBankPlatform(bankPlatform);
    }

    /**
     * 修改银行账户平台
     * 
     * @param bankPlatform 银行账户平台
     * @return 结果
     */
    @Override
    public int updateBankPlatform(BankPlatform bankPlatform)
    {
        bankPlatform.setUpdateTime(DateUtils.getNowDate());
        return bankPlatformMapper.updateBankPlatform(bankPlatform);
    }

    /**
     * 批量删除银行账户平台
     * 
     * @param bankPlatformAcctids 需要删除的银行账户平台主键
     * @return 结果
     */
    @Override
    public int deleteBankPlatformByBankPlatformAcctids(Long[] bankPlatformAcctids)
    {
        return bankPlatformMapper.deleteBankPlatformByBankPlatformAcctids(bankPlatformAcctids);
    }

    /**
     * 删除银行账户平台信息
     * 
     * @param bankPlatformAcctid 银行账户平台主键
     * @return 结果
     */
    @Override
    public int deleteBankPlatformByBankPlatformAcctid(Long bankPlatformAcctid)
    {
        return bankPlatformMapper.deleteBankPlatformByBankPlatformAcctid(bankPlatformAcctid);
    }
}
